/**
 * bootstrap editable plugins
 */
(function() {
	'use strick',
	
	require.config({
		paths: {
			"bootstrap.editable": "bootstrap/extends/bootstrap-editable/1.5.1/js/bootstrap-editable.min",
			"bootstrap.editable.select": "miwawa/1.0.2/js/jquery/select2/4.0.2/js/select2",
			"bootstrap.editable.select.zh-CN": "miwawa/1.0.2/js/jquery/select2/4.0.2/js/i18n/zh-CN",
			"bootstrap.editable.combotext": "bootstrap/extends/bootstrap-editable/1.5.1/js/ext/combotext"
		},
		shim: {
			"bootstrap.editable": {
				deps : [
				   "jquery", 
				   "css!bootstrap/extends/bootstrap-editable/1.5.1/css/bootstrap-editable.css"
				],
				exports:"jQuery.fn.editable"
			},
			"bootstrap.editable.select": {
				deps : [
				   "jquery", 
				   "css!miwawa/1.0.2/js/jquery/select2/4.0.2/css/select2.min.css",
				   "css!miwawa/1.0.2/js/jquery/select2/4.0.2/css/select2-bootstrap.css",
				]
			},
			"bootstrap.editable.select.zh-CN": {
				deps : ["bootstrap.editable.select"]
			},
			"bootstrap.editable.combotext": {
				deps : ["bootstrap.editable"],
				exports:"jQuery.fn.editabletypes.combotext"
			}
		}
	});
	
	define(['bootstrap.editable.select','bootstrap.editable.select.zh-CN','bootstrap.editable','bootstrap.editable.combotext'], function(){
		return {
			register:function(elem, options){
				jQuery(elem).editable(jQuery.extend({url:handlerUpdate},options));
			}
		};
		
		function handlerUpdate(params){
			var opts = jQuery(this).data();
			var bean = {};
			var callback = opts.callback;
			if(typeof params.value == "object"){
				jQuery.each(params.value,function(key,val){
					bean[key] = val;
				});
			}else{
				bean[params.name] = params.value;
				if(opts["other"]){
				    /*var	temp = eval("("+opts.other+")");
				    jQuery.each(temp,function(key,val){
						bean[key] = val;
					});*/
					jQuery.extend(bean,eval("("+opts.other+")"));
				}
			}
			bean["id"] = params.pk;
			
			jQuery.ajaxsend({
				url:"/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
				data:{
					"entityClass":jQuery(this).data("entity"),
					"bean":bean
				},
				success:function(){
					if(callback) window[callback](bean.year)
				}
			});
		}
	});
})(this);
